﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using eDoc.BO;
namespace eDoc
{
    public partial class logindownload : System.Web.UI.Page
    {
        private int id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] != null && Request.QueryString["id"].ToString() != "")
            {
                id = Convert.ToInt32(Request.QueryString["id"]);
            }

            if (CheckDownloadCounter())
            {
                BindData();
            }
            else
            {
                DownloadLimit();
            }
        }

        private bool CheckDownloadCounter()
        {
            bool flag = true;
            string partnerCode = BO.Common.PartnerCode;
            DataTable dt = new DataTable();
            dt = BO.Data.Select_DownloadCounter(partnerCode);
            if (dt != null && dt.Rows.Count > 0)
            {
                int counter = Convert.ToInt32(dt.Rows[0]["Counter"]);
                int counterLimit = Convert.ToInt32(dt.Rows[0]["CounterLimit"]);
                if (counterLimit != -1)
                {
                    if (counter >= counterLimit)
                    {
                        flag = false;
                    }
                    else
                    {
                        BO.Data.Insert_DownloadCounter(partnerCode);
                    }
                }
                else
                {
                    BO.Data.Insert_DownloadCounter(partnerCode);
                }
            }
            else
            {
                BO.Data.Insert_DownloadCounter(partnerCode);
            }
            return flag;
        }

        private void BindData()
        {
            DataTable dt = new DataTable();
            dt = Data.Select_AppPlist_by_ID(id);
            if (dt != null && dt.Rows.Count > 0)
            {
                string filePlist = dt.Rows[0]["AppPlistPath"].ToString().Replace("fileinfo","fileplist");                
                string appkey = dt.Rows[0]["AppKey"].ToString();
                Response.Redirect("/plist.aspx?ak=" + appkey);
                //Download(appkey);                
            }
        }

       
        private void ReadPlist(string pathFilePlist)
        {
            BO.PList pl = new BO.PList();
            pl.Load(pathFilePlist);

            string value = "";
            string key = "";
            List<dynamic> list = new List<dynamic>();
            Dictionary<string, string> dict = new Dictionary<string, string>();
            
        }

        private void Download(string appKey)
        {
            DataTable dt = new DataTable();
            dt = Data.Select_AppPlist_by_ID(id);
            if (dt != null && dt.Rows.Count > 0)
            {
                string appkey = dt.Rows[0]["AppKey"].ToString();
                Session["DownloadApp"] = appkey;
                if (CheckDownloadCounter())
                {
                    Response.Redirect("/plist.aspx?ak=" + appkey);
                    //LoginDownloadSuccess(appkey);
                }
                else
                {
                    DownloadLimit();
                }
                //Response.Redirect("/plist.aspx?ak=" + appkey);
            }
        }

        protected void btLogin_Click(object sender, EventArgs e)
        {            
            string user = txtUser.Text.ToLower().Trim();
            string pass = txtPass.Text.Trim();

            if (user == "")
            {
                lblNote.Text = "Bạn chưa nhập tên tài khoản!";
                txtUser.Focus();
                return;
            }

            if (pass == "")
            {
                lblNote.Text = "Bạn chưa nhập mật khẩu!";
                txtPass.Focus();
                return;
            }

            string strpassmd5 = Encryptor.GetMd5Hash(pass);
            int result = Data.LoginDownload(user, strpassmd5);
            if (result == 0)
            {
                lblNote.Text = "Bạn nhập sai tên tài khoản!";
                txtUser.Focus();
                return;
            }
            else if (result == 1)
            {
                lblNote.Text = "Bạn nhập sai mật khẩu!";
                txtPass.Focus();
                return;
            }
            else if (result == 2)
            {
                DataTable dt = new DataTable();
                dt = Data.Select_AppPlist_by_ID(id);
                if (dt != null && dt.Rows.Count > 0)
                {
                    string appkey = dt.Rows[0]["AppKey"].ToString();
                    Session["DownloadApp"] = appkey;
                    if (CheckDownloadCounter())
                    {                        
                        LoginDownloadSuccess(appkey);
                    }
                    else
                    {
                        DownloadLimit();
                    }
                    //Response.Redirect("/plist.aspx?ak=" + appkey);
                }
                else
                {
                    lblNote.Text="Không tồn tại ứng dụng!";
                }
            }
           
        }

        private void LoginDownloadSuccess(string key)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append(@"<script type='text/javascript'>");
            sb.Append(@"parent.LoginDownloadSuccess('"+ key +"');");
            sb.Append(@"</script>");
            Page.RegisterClientScriptBlock("LoginDownloadSuccess", sb.ToString());
        }

        private void DownloadLimit()
        {
            /*
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append(@"<script type='text/javascript'>");
            sb.Append(@"alert('Bạn đã quá giới hạn tải ứng dụng!'); parent.");
            sb.Append(@"</script>");
            Page.RegisterClientScriptBlock("DownloadLimit", sb.ToString());
            */

            loginbox.Visible = false;
            loginNote.Visible = true;
            lblNoteLimit.Text = "Bạn đã tải ứng dụng quá giới hạn cho phép!";
        }
    }
}